package com.hanlin.backend.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 对filter、servlet、listener进行注册的配置类
 *
 * @author:hl.yuan
 * @date：2020-12-22
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {

    @Value("${frontend.allowedOrigins}")
    private String origins;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //本应用的所有方法都会去处理跨域请求
        registry.addMapping("/**")
                //允许远端访问的域名
                .allowedOrigins(origins)
                //允许请求的方法("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedMethods("*")
                //允许请求头
                .allowedHeaders("*");
    }

}
